 //增加元素之前，检查id是否重复的方法
function checkIdBeforeAdd() {
    let studentId = $("input[name='studentId']").val();
    let url = `checkStudent?studentId=${studentId}`;
    $.ajax({
        type: "get",
        url: url,
        dataType: "text",
        success: function (data) {
            if (data === "true") {
                $("#studentIdExistMark").show();
            }else {
                $("#studentIdExistMark").hide();
            }
        },
        error: function (xhr, type, errorThrown) {
            console.log("xhr:" + xhr + " type:" + type + " errorThrown" + errorThrown);
        }
    });
}

 //增加班号之前，检查cid是否重复的方法
 function checkCidBeforeAdd() {
     let studentCid = $("input[name='studentCid']").val();
     let url = `checkClassBean?id=${studentCid}`;
     $.ajax({
         type: "get",
         url: url,
         dataType: "text",
         success: function (data) {
             if (data === "true") {
                 $("#studentCidExistMark").hide();
             }else {
                 $("#studentCidExistMark").show();
             }
         },
         error: function (xhr, type, errorThrown) {
             console.log("xhr:" + xhr + " type:" + type + " errorThrown" + errorThrown);
         }
     });
 }

$(function () {
    //为展示全部按钮绑定事件
    $("#showAll").click(function () {
        location.href = "getUserByPage";
    });

    //退出登录功能
    $("#logout").click(function () {
        location.href = "logout";
    });

    //删除按钮绑定事件
    $("#deleteRows").click(function () {
        $(".checkbox:checked").each(function () {
            let trObj = $(this).parents("tr");
            let studentId = trObj.children(".tdId").text();
            let url = `removeStudent?studentId=${studentId}`;
            $.ajax({
                type: "get",
                url: url,
                dataType: "text",
                success: function (data) {
                    if (data === "true") {
                        trObj.remove();
                        location.href = "getUserByPage";
                    }else {
                        alert("删除失败，学生不存在");
                    }
                },
                error: function (xhr, type, errorThrown) {
                    console.log("xhr:" + xhr + " type:" + type + " errorThrown" + errorThrown);
                }
            });
        });
    });


    //为确定增加按钮绑定事件
    $("#addRow").click(function () {
        let studentId = $("input[name='studentId']").val();
        let studentCid = $("input[name='studentCid']").val();
        let studentName = $("input[name='studentName']").val();
        let studentEmail = $("input[name='studentEmail']").val();
        let studentComment = $("input[name='studentComment']").val();
        let studentBirthday = $("input[name='studentBirthday']").val();
        let studentGender = $("input[name='studentGender']:checked").val();

        if (!studentId) {
            alert("添加失败，学号不能为空且不能为非数字");
            return;
        }

        if (!studentCid) {
            alert("添加失败，班号不能为空且不能为非数字");
            return;
        }

        if (!studentName) {
            alert("添加失败，姓名不能为空");
            return;
        }

        if (!studentEmail) {
            alert("添加失败，邮箱不能为空");
            return;
        }

        if (!studentComment) {
            alert("添加失败，备注不能为空");
            return;
        }

        if (!studentBirthday) {
            alert("添加失败，出生日期不能为空");
            return;
        }

        if (!studentGender) {
            alert("添加失败，姓别不能为空");
            return;
        }

        //如果学号存在，会提示已存在
        if ($("#studentIdExistMark").css("display") !== "none") {
            alert("添加失败，学号不能重复");
            return;
        }

        //如果学号不存在，会提示不存在
        if ($("#studentCidExistMark").css("display") !== "none") {
            alert("添加失败，班号不存在");
            return;
        }

        //关闭模态框，发送数据到后台服务器
        $('#modalAdd .close').click();
        location.href = `addStudent?id=${studentId}&cid=${studentCid}&name=${studentName}&gender=${studentGender}&birthday=${studentBirthday}&email=${studentEmail}&comment=${studentComment}`;
    });

    //为编辑事件绑定按钮
    $("#editRows").click(function () {
        $(".checkbox:checked").each(function () {
            //获取包含复选框的td对象
            let checkboxObj = $(this).parent();

            //新建一个完成按钮
            let finishButton = $("<button class='btn btn-success btn-xs'>完成</button>");

            //修改td对象的内容
            checkboxObj.html("");
            //添加完成按钮
            checkboxObj.append(finishButton);
            //为其他同辈td进入编辑模式
            checkboxObj.siblings().not("td:eq(0)").each(function () {
                let tdObj = $(this);
                let text = tdObj.html();
                //清空td中的内容
                tdObj.html("");
                //插入一个文本框到td中
                $("<input type='text'>")
                    .css("background-color", tdObj.css("background-color"))
                    .width("10rem")
                    .val(text)
                    .appendTo(tdObj);
            });

            //为完成按钮增加点击事件
            finishButton.click(function () {
                let trObj = $(this).parents("tr");
                let studentId = trObj.children(".tdId").text();
                let studentCid = trObj.children(".tdCid").children().val();
                let studentName = trObj.children(".tdName").children().val();
                let studentEmail = trObj.children(".tdEmail").children().val();
                let studentComment = trObj.children(".tdComment").children().val();
                let studentBirthday = trObj.children(".tdBirthday").children().val();
                let studentGender = trObj.children(".tdGender").children().val();
                let url = `updateStudent?id=${studentId}&cid=${studentCid}&name=${studentName}&gender=${studentGender}&birthday=${studentBirthday}&email=${studentEmail}&comment=${studentComment}`;
                $.ajax({
                    type: "get",
                    url: url,
                    dataType: "text",
                    success: function (data) {
                        if (data === "true") {
                            //恢复td对象
                            checkboxObj.html("");
                            checkboxObj.append("<input class='checkbox' type='checkbox'>");
                            $(checkboxObj.siblings()).not("td:eq(0)").each(function () {
                                let tdObj = $(this);
                                //将td的内容修改为文本框的内容
                                tdObj.html(tdObj.children().val());
                            });
                        }else {
                            alert("修改失败");
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log("xhr:" + xhr + " type:" + type + " errorThrown" + errorThrown);
                        alert("服务器错误")
                    }
                });
            });
        });
    });
});